<template>
    <header class="header">
        <div class="nav-container">
            <div class="logo">
                <a href="/"><img src="../assets/logo1.jpg" alt="菲斯生物" /></a>
                <a href="/"><h1>菲斯生物</h1></a>
            </div>
            <div class="hamburger" @click="openMenu">
                <span></span>
                <span></span>
                <span></span>
            </div>
            <ul class="nav-menu">
                <li v-for="menu in menus" :key="menu.name">
                    <a :href="`/${menu.name}`" :class="{ active: activeName === menu.name }">{{ menu.label }}</a>
                </li>
            </ul>
        </div>

        <!-- 移动端的菜单 -->
        <el-dialog :visible.sync="dialogTableVisible" custom-class="menu-dialog" :append-to-body="true" width="100%">
            <ul>
                <li v-for="menu in menus" :key="menu.name">
                    <a :href="`/${menu.name}`" :class="{ active: activeName === menu.name }">{{ menu.label }}</a>
                </li>
            </ul>
        </el-dialog>
        <div class="search-container">
            <div class="search-form">
                <el-input
                    placeholder="产品或文章均含产品，请选择并输入产品货号，CAS#,  中文或英文品名搜索"
                    v-model="key"
                    class="input-with-select"
                >
                    <el-select v-model="type" slot="prepend" placeholder="Select">
                        <el-option label="产品" :value="1"></el-option>
                        <el-option label="文章" :value="2"></el-option>
                    </el-select>
                    <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
                </el-input>
            </div>
        </div>
    </header>
</template>
<script setup>
import { ref } from 'vue'
import router from '@/router/index'
import { Message } from 'element-ui'
const activeName = ref('')
router.onReady(() => {
    const currentRoute = router.currentRoute
    activeName.value = currentRoute.meta?.parent || currentRoute.name || 'home'
})
const menus = [
    {
        label: '首页',
        name: 'home',
    },
    {
        label: '关于我们',
        name: 'about',
    },
    {
        label: '定制合成',
        name: 'synthesis',
    },
    {
        label: '产品中心',
        name: 'products',
    },
    {
        label: '新闻中心',
        name: 'news',
    },
    {
        label: '招贤纳士',
        name: 'jobs',
    },
    {
        label: '联系我们',
        name: 'contact',
    },
]

// 菜单
const dialogTableVisible = ref(false)
function openMenu() {
    dialogTableVisible.value = true
}

const type = ref(1)
const key = ref('')
const search = () => {
    if (!key.value) {
        return Message.info('请输入关键字！')
    }
    if (type.value === 1) {
        // 搜索产品
        router.push({
            name: 'product-search',
            query: {
                keyword: key.value,
            },
        })
    } else {
        router.push({
            name: 'news-search',
            query: {
                keyword: key.value,
            },
        })
    }

    key.value = ''
}
</script>
<style scoped>
.header {
    width: 100%;
    /* border-bottom: 1px solid #e5e5e5; */
    background: #fff;
}

.nav-container {
    max-width: 1200px;
    margin: 0 auto;
    height: 60px;
    position: relative;
}

.logo {
    float: left;
    line-height: 60px;
    a {
        display: block;
        float: left;
    }
    img {
        width: 81px;
        height: auto;
        padding-top: 5px;
    }
    h1 {
        padding-left: 10px;
        font-size: 22px;
        color: rgb(9, 164, 195);
    }
}

.logo img {
    height: 40px;
    display: block;
    vertical-align: middle;
}

.nav-menu {
    float: right;
    list-style: none;
    margin: 0;
    padding: 0;
}

.nav-menu:hover a.active {
    background: none;
    font-weight: normal;
    color: #333;
}

.nav-menu:hover li:hover a {
    color: #fff;
    background: #5bc0de;
}

.nav-menu li {
    float: left;
    position: relative;
}

.nav-menu a {
    display: block;
    padding: 0 25px;
    line-height: 60px;
    color: #333;
    text-decoration: none;
    font-size: 16px;
}

.nav-menu a:hover,
.nav-menu a.active {
    background: #5bc0de;
    color: #fff;
    font-weight: 700;

    background-image: linear-gradient(
        165deg,
        rgba(255, 255, 255, 0.25) 0%,
        rgba(255, 255, 255, 0.1) 30%,
        rgba(0, 0, 0, 0.05) 70%,
        rgba(0, 0, 0, 0.1) 100%
    );
}

.hamburger {
    display: none;
    position: absolute;
    right: 0;
    top: 0;
    height: 61px;
    cursor: pointer;
    padding: 15px;
    background-color: #09a4c3;
}

.hamburger span {
    display: block;
    width: 25px;
    height: 3px;
    background: #fff;
    margin: 5px 0;
    border-radius: 2px;
}

/* Clear fix for IE9 */
.nav-container:after {
    content: '';
    display: table;
    clear: both;
}

.search-container {
    /* banner 中展示 */
    /* position: absolute;
    top: 150px; */
    width: 100%;
    text-align: center;
    height: 60px;
    line-height: 60px;
    background-color: rgb(249, 249, 249);
    z-index: 10;

    .search-form {
        width: 50%;
        margin: 0 auto;

        .input-with-select .el-select {
            width: 100px;
        }
    }
}

@media screen and (max-width: 768px) {
    .hamburger {
        display: block;
    }

    .nav-menu {
        display: none;
        float: none;
        width: 100%;
        position: absolute;
        top: 80px;
        left: 0;
        background: #fff;
        border-top: 1px solid #e5e5e5;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    }

    .nav-menu.active {
        display: block;
    }

    .nav-menu li {
        float: none;
        border-bottom: 1px solid #e5e5e5;
    }

    .nav-menu a {
        line-height: 50px;
        padding: 0 15px;
    }

    /* 搜索框 */
    .search-container {
        /* position: absolute;
        top: 150px; */
        /* position: absolute;
        top: 80px; */
        width: 100%;
    }
    .search-form {
        width: 88% !important;
    }
}

/* Fallback for IE9 flex support */
.ie9 .nav-container {
    text-align: justify;
}

.ie9 .nav-container:after {
    content: '';
    display: inline-block;
    width: 100%;
}
</style>

<style lang="scss">
/* 移动端菜单弹窗 */
.menu-dialog {
    margin-top: 60px !important;
    width: 100% !important;

    .el-dialog__header {
        height: 0 !important;
        padding: 0 !important;
    }
    .el-dialog__body {
        padding: 0 16px !important;
    }

    ul {
        li {
            text-decoration: none;
            height: 35px;
            line-height: 35px;
            font-size: 16px;
            list-style: none;
            text-align: center;
            border-bottom: 1px solid #eeeeee;

            a {
                color: #606266 !important;
            }
        }
    }

    // 关闭按钮
    .el-dialog__headerbtn {
        top: -52px !important;
        right: 4px;
        width: 47px;
        height: 45px !important;
        font-size: 40px;
        background: rgb(9, 164, 195) none repeat scroll 0% 0% / auto padding-box border-box;
        color: rgb(255, 255, 255) !important;

        i {
            color: rgb(255, 255, 255) !important;
        }
    }
}
</style>
